//
//  MaTool.m
//  test
//
//  Created by 栋 on 2018/9/26.
//  Copyright © 2018年 com.pulian. All rights reserved.
//

#import "MaTool.h"
#import "MATabBarViewController.h"
#import "AFNetworking.h"


@implementation MaTool

+ (NSString *)getSafeStringWith:(NSString *)string{
    if (!string) {
        return @"";
    }
    if ([string isEqual:nil]) {
        return @"";
    }
    if ([string isEqual:@""]) {
        return @"";
    }
    if ([string isEqual:@"(null)"]) {
        return @"";
    }
    id tempStr=string;
    if (tempStr==[NSNull null]) {
        return @"";
    }
    if ([tempStr isKindOfClass:[NSNumber class]]) {
        return [NSString stringWithFormat:@"%@",string];
    }
    return string;
}

+(BOOL)isEmptyStr:(NSString *) string
{
    if (!string) {
        return YES;
    }
    if ([string isEqual:nil]) {
        return YES;
    }
    if ([string isEqual:@""]) {
        return YES;
    }
    if ([string isEqual:@"(null)"]) {
        return YES;
    }
    id tempStr=string;
    if (tempStr==[NSNull null]) {
        return YES;
    }
    
    return NO;
}

+ (nullable id)sy_getFileWithName:(NSString *)name
{
    NSString * childVCsJsonPath = [[NSBundle mainBundle] pathForResource:name ofType:nil];
    NSData *childVCsJSONData = [NSData dataWithContentsOfFile:childVCsJsonPath];
    if (!childVCsJSONData) {
        return nil;
    }
    id childVCsDict = [NSJSONSerialization JSONObjectWithData:childVCsJSONData options:NSJSONReadingAllowFragments error:nil];
    return childVCsDict;
}

+(NSDate*)dateFromString:(NSString*)dateString WithFormatterString:(NSString*)formatterString
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:formatterString];
    [formatter setTimeZone:[self getSYTimeZone]];
    NSDate *date=[formatter dateFromString:dateString];
    return date;
}

+ (NSString *)stringFromDate:(NSDate *)date WithFormatterString:(NSString*)formatterString{
    
    
    
    NSDateFormatter *dateFormatter = [self getFormatterWithFormatString:formatterString];
    //    NSString * localStr = [NSBundle getCusLanguage];
    //    if (localStr) {
    //        if ([localStr containsString:@"zh"]) {
    //            [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hant"]];
    //        }else{
    //            [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_us"]];
    //        }
    //    }
    
    NSString *destDateString = [dateFormatter stringFromDate:date];
    
    return destDateString;
    
}


+ (NSString *)stringFromTimeInterval:(NSTimeInterval)timeInterval WithFormatterString:(NSString*)formatterString{
    NSTimeInterval time = timeInterval / 1000.0;
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:time];
    return [self stringFromDate:confromTimesp WithFormatterString:formatterString];
}

+ (NSString *)stringWeekDayFromTimeInterval:(NSTimeInterval)timeInterval
{
    if (timeInterval > pow(10, 11)) {
//        是13位的毫秒
        timeInterval=timeInterval/1000.0;
    }
    
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:timeInterval];

    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *weekdayComponents =
    [gregorian components:NSCalendarUnitWeekday fromDate:date];
    NSInteger _weekday = [weekdayComponents weekday];
    NSString *weekStr;
    if (_weekday == 1) {
        weekStr = @"星期日";
    }else if (_weekday == 2){
        weekStr = @"星期一";
    }else if (_weekday == 3){
        weekStr = @"星期二";
    }else if (_weekday == 4){
        weekStr = @"星期三";
    }else if (_weekday == 5){
        weekStr = @"星期四";
    }else if (_weekday == 6){
        weekStr = @"星期五";
    }else if (_weekday == 7){
        weekStr = @"星期六";
    }
    return weekStr;
}

+(NSDateFormatter *)getFormatterWithFormatString:(NSString *)formatterString{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:formatterString];
    
    [dateFormatter setTimeZone:[self getSYTimeZone]];
    return dateFormatter;
}

+(NSTimeZone *)getSYTimeZone{
    return [NSTimeZone systemTimeZone];
}

+ (void)networkReachability:(void(^)(bool reachable))block
{
    //创建网络监听对象
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    //开始监听
    [manager startMonitoring];
    //监听改变
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                block(NO);
                break;
            case AFNetworkReachabilityStatusNotReachable:
                block(NO);
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                block(YES);
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                block(YES);
                break;
        }
    }];
    //结束监听
    [manager stopMonitoring];
}

+(id)getConfigFromOptionSettingWithKey:(NSString *)key{
    NSString *optionPath = [[NSBundle mainBundle] pathForResource:@"OptionSetting.plist" ofType:nil];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:optionPath];
    NSString *restfulInterface = [dic objectForKey:key];
    return restfulInterface;
}

+(void)showAlertWithTitle:(NSString *)title{
    [self showAlertWithTitle:title completion:nil];
}

+(void)showAlertWithTitle:(NSString *)title completion:(void (^ __nullable)(void))completion{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:title preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (completion) {
            completion();
        }
    }];
    [alert addAction:action];
    [[self getCurrectDisplayViewController] presentViewController:alert animated:YES completion:nil];
}

+(void)showAlertChooseWithTitle:(NSString *)title message:(NSString *)message makeSureBlock:(void (^ __nullable)(void))makeSureBlock  cancelBlock:(void (^ __nullable)(void))cancelBlock{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:title preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (makeSureBlock) {
            makeSureBlock();
        }
    }];
    UIAlertAction * actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (cancelBlock) {
            cancelBlock();
        }
    }];
    [alert addAction:action];
    [alert addAction:actionCancel];
    [[self getCurrectDisplayViewController] presentViewController:alert animated:YES completion:nil];
}

+ (UIViewController *)getCurrectDisplayViewController {
    UIViewController *resultVC;
    resultVC = [self loopGetCurrentVC:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self loopGetCurrentVC:resultVC.presentedViewController];
    }
    return resultVC;
}
+ (UIViewController *)loopGetCurrentVC:(UIViewController *)vc {
    
        if ([vc isKindOfClass:[UINavigationController class]]) {
            return [self loopGetCurrentVC:[(UINavigationController *)vc topViewController]];
        } else if ([vc isKindOfClass:[MATabBarViewController class]]) {
            return [self loopGetCurrentVC:[(MATabBarViewController *)vc selectedViewController]];
        }else {
            return vc;
        }

//    if ([vc isKindOfClass:[UINavigationController class]]) {
//        return [self loopGetCurrentVC:[(UINavigationController *)vc topViewController]];
//    } else if ([vc isKindOfClass:[UITabBarController class]]) {
//        return [self loopGetCurrentVC:[(UITabBarController *)vc selectedViewController]];
//    }else {
//        return vc;
//    }

    return nil;
    
}
@end
